function yerefresh(){
    location.reload(true);
}
function hide(){
    if($("#searchheader").css("display")=="none"){
        $("#searchheader").css("display","block");
        $("#hide").text("收起筛选")
    }else if($("#searchheader").css("display")=="block"){
        $("#searchheader").css("display","none");
        $("#hide").text("打开筛选")
    }
}
function qbkc(){
    $("#ysj").css("background-color","");
    $("#wsj").css("background-color","");
    $("#dsh").css("background-color","");
    $("#wtg").css("background-color","");
    $("#qbkc").css("background-color","pink");
    layui.use('table', function () {
        $("#mark").val(0);
        var table=layui.table;
        table.reload('demo',{
            where:{
                mark:0
            }
        })
    })
}
function ysj(){
    $("#wsj").css("background-color","");
    $("#dsh").css("background-color","");
    $("#wtg").css("background-color","");
    $("#qbkc").css("background-color","");
    $("#ysj").css("background-color","purple");
    layui.use('table', function () {
        $("#mark").val(1);
        var table=layui.table;
        table.reload('demo',{
            where:{
                mark:1
            }
        })
    })
}
function wsj(){
    $("#dsh").css("background-color","");
    $("#wtg").css("background-color","");
    $("#qbkc").css("background-color","");
    $("#ysj").css("background-color","");
    $("#wsj").css("background-color","blue");
    layui.use('table', function () {
        $("#mark").val(2);
        var table=layui.table;
        table.reload('demo',{
            where:{
                mark:2
            }
        })
    })
}
function dsh(){
    $("#wtg").css("background-color","");
    $("#qbkc").css("background-color","");
    $("#ysj").css("background-color","");
    $("#wsj").css("background-color","");
    $("#dsh").css("background-color","orange");
    layui.use('table', function () {
        $("#mark").val(3);
        var table=layui.table;
        table.reload('demo',{
            where:{
                mark:3
            }
        })
    })
}
function wtg(){
    $("#qbkc").css("background-color","");
    $("#ysj").css("background-color","");
    $("#wsj").css("background-color","");
    $("#dsh").css("background-color","");
    $("#wtg").css("background-color","red");
    layui.use('table', function () {
        $("#mark").val(43);
        var table=layui.table;
        table.reload('demo',{
            where:{
                mark:4
            }
        })
    })
}
function gj(classify,name,sale,scname,status,auditstatus){
    var mark=$("#mark").val();
    console.log(mark)
    if(mark === ''|| mark === null || mark === undefined){
        mark=0
    }
    layui.use('table', function () {
        var table=layui.table;
        table.reload('demo',{
            where:{
                classify:classify,
                name:name,
                sale:sale,
                scname:scname,
                status:status,
                auditstatus:auditstatus,
                mark:mark
            }
        })
    })
}
function advancedsearch(){
    layer.open({
        type: 2,
        title: '高级检索',
        content: "../coursepage/advancedsearch.html",
        area: ['80%', '80%'],
        // end:function(){
        //
        // }
    })
}
function aa(res1,res2) {
    if(res1==0){
        return '<div style="line-height: 50px">'+'未审核' + '<br/>' + '<a className="layui-btn" onClick="auditprocess('+res2+');">'+'审核详情'+'</a>'+'</div>';
    }else if(res1==1){
        return '<div style="line-height: 50px">'+'已通过' + '<br/>' + '<a className="layui-btn" onClick="auditprocess('+res2+');">'+'审核详情'+'</a>'+'</div>';
    }else if(res1==2){
        return '<div style="line-height: 50px">'+'未通过' + '<br/>' + '<a className="layui-btn" onClick="auditprocess('+res2+');">'+'审核详情'+'</a>'+'</div>';
    }
}
function auditprocess(id){
    localStorage.setItem("auditprocessid",id);
    layer.open({
        type: 2,
        title: '审核详情',
        content: "../courseaudit/auditprocess.html",
        area: ['80%', '80%'],
    })
}
    window.name="upload";
function refresh1() {
    tableIn.reload("demo");
}
var tableIn=null;
var kcname=null;
var kcprice=null;
var treenode11=null;
var title1=null;
var data1=null;
$.ajax({
    type:'post',
    url: '/advanced/selectallheader',
    success:function(res){
        console.log(res.selectallcourse);
        console.log(res.selectdsh);
        console.log(res.selectwsj);
        console.log(res.selectwtg);
        console.log(res.selectysj);
        $("#selectallcourse").text(res.selectallcourse);
        $("#selectdsh").text(res.selectdsh);
        $("#selectwsj").text(res.selectwsj);
        $("#selectwtg").text(res.selectwtg);
        $("#selectysj").text(res.selectysj);
        $("#qbkc").css("background-color","pink");
    }
})
myajax = $.ajax({
    type:'post',
    url: '/advanced/getxialatree',
    success:function(res){
        data1=res.data;
        console.log(data1)
    }
})
$.when(myajax).done(function () {
    layui.use(['table','dropdown','form'], function () {
        console.log(data1)
        var form=layui.form
        var dropdown = layui.dropdown
        var table = layui.table;
        table.on('tool(test)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            var tr = obj.tr;

            if(layEvent === 'detail'){ //查看
                $.ajax({
                    type:'post',
                    url: '/advanced/addoperatelog',
                    data:{operatelist:'课程列表',operatetype:'查看',operateremark:'查看课程详情'},
                    success:function(res){
                        if(top.window['refresh']){
                            top.window['refresh'].frames.location.reload();
                        }
                    }
                })
                console.log(data);
                console.log(data.id)
                localStorage.setItem("selectid",data.id)
                //do somehing
                layer.open({
                    type: 2,
                    title: '查看课程',
                    content: "selectcourse.html",
                    area: ['80%', '80%'],
                })
            } else if(layEvent === 'delete'){ //删除
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        type:'post',
                        url: '/advanced/addoperatelog',
                        data:{operatelist:'课程列表',operatetype:'删除',operateremark:'删除课程详情'},
                        success:function(res){
                            if(top.window['refresh']){
                                top.window['refresh'].frames.location.reload();
                            }
                        }
                    }),
                    // 向服务端发送删除指令
                    console.log(data.id);
                    $.ajax({
                        type: 'post',
                        url: '/advanced/delcoursetable',
                        data: {id:data.id},
                        dataType: 'json',
                        success: function (res) {
                            if(res.state==200){
                                obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                layer.close(index);
                            }
                        }
                    })

                });
            } else if(layEvent === 'update'){ //编辑
                $.ajax({
                    type:'post',
                    url: '/advanced/addoperatelog',
                    data:{operatelist:'课程列表',operatetype:'编辑',operateremark:'编辑课程详情'},
                    success:function(res){
                        if(top.window['refresh']){
                            top.window['refresh'].frames.location.reload();
                        }
                    }
                }),
                console.log(data);
                console.log(data.id)
                localStorage.setItem("selectid",data.id)
                //do somehing
                layer.open({
                    type: 2,
                    title: '编辑',
                    content: "editcourse.html",
                    area: ['80%', '80%'],
                })
            } else if(layEvent === 'log'){
                localStorage.setItem("logid",data.id)
                layer.open({
                    type: 2,
                    title: '课程日志',
                    content: "courselog.html",
                    area: ['80%', '80%'],
                })
            }
        });

        dropdown.render({
            elem: '#demo1', //可绑定在任意元素中，此处以上述按钮为例
            data:data1
            ,id: 'demo1'
            //菜单被点击的事件
            ,click: function(obj){
                var mark=$("#mark").val();
                if(mark==""){
                    mark=0;
                }
                console.log(obj.title);
                title1=obj.title;
                table.reload('demo',{
                    where:{
                        classify:title1,
                        mark:mark
                    }
                })
                // layer.msg('回调返回的参数已显示再控制台');
            }
        });
        //监听开关按钮
        form.on('switch(stat)', function(obj){
            var id = $(this).attr('menuId');//为了获取开关所在行的信息，需要绑定上所在行的id
            console.log('id:' + id);
            var isManager = obj.elem.checked?"1":"0";
            console.log('isManager:' + isManager);
            $.ajax({
                type:'post',
                data: {id:id,status:isManager},
                url: '/advanced/updateswitch',
                success:function(res){

                }
            })
        })

        //第一个实例
        tableIn = table.render({
            elem: '#demo'
            , height: 800
            , id: 'demo'
            , url: '/advanced/gettable' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: '编号',width:100},
                {field: 'piano',width:100, title: '课程图片',templet:function(res){
                        return '<div><img src="'+res.piano+'" width="70px" height="70px"></a></div>';
                    }},
                {field: 'name',width:100, title: '课程名称'},
                {field: 'scname',width:100, title: '上传人'},
                {field: 'price',width:100, title: '价格'},
                {field: 'status',width:100, title: '状态',templet:function(res){
                        let menuId=res.id;
                        if (res.status == 1) {
                            return "<input type='checkbox' menuId='" + menuId + "' lay-filter='stat'  lay-skin='switch' lay-text='上架|下架' checked>";
                        }
                        else if(res.status == 0){
                            return "<input type='checkbox' menuId='" + menuId + "' lay-filter='stat'  lay-skin='switch' lay-text='上架|下架'>";
                        }
                    }},
                {field: 'classify',width:100, title: '分类'},
                {field: 'sale',width:100, title: '销量'},
                {field: 'appstatus',width:100, title: '审核状态',
                    templet: function (d) {return aa(d.appstatus,d.id)}},
                {title: '操作',width:150, toolbar: '#barDemo'}
            ]],
                // templet:function(res){
                //     if(res.appstatus==0){
                //         return '未审核';
                //     }else{
                //         return '已审核';
                //     }
                // }},
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.state, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
        });
        $("#selectkc").click(function(){
            kcname=$("#kcname").val();
            var mark=$("#mark").val();
            if(mark === ''|| mark === null || mark === undefined){
                mark=0
            }
            kcprice=$("#kcprice").val();
            console.log(11111111111111);
            console.log(kcname);
            console.log(kcprice);
            console.log(111111111111111);
            table.reload('demo',{
                where:{
                    name:kcname,
                    classify:title1,
                    price:kcprice,
                    mark:mark
                }
            })
        })
    });
    $(document).ready(function () {
        $.ajax({
            type: "get",
            url: "/intermediate/gettree",
            async: true,
            success: function (res) {
                console.log(res.menulists)
                zTreeObj = $.fn.zTree.init($("#treeDemo"), {
                    async: {
                        enable: true
                    },
                    edit: {
                        // enable: true,
                        drag: {
                            isMove: true, // 打开移动功能，鼠标左键点击后拖拽
                            prev: true,
                            autoOpenTime: 0
                        },
                        editNameSelectAll: true,
                        showRemoveBtn: true,
                        removeTitle: "删除节点",
                        renameTitle: "编辑节点名称",
                        showRenameBtn: true,
                    },
                    view: {
                        selectedMulti: false,
                        showIcon: true,// 显示图标
                        showLine: true,
                        // addHoverDom: addHoverDom
                        // removeHoverDom : removeHoverDom
                    },
                    data: {
                        simpleData: {
                            enable: true,
                            idKey: 'id',
                            pIdKey: 'pid',
                            rootPId: 0
                        },
                        key: {
                            name: "name"
                        }
                    },
                    check: {
                        enable: true,
                        chkStyle: "checkbox",
                        chkboxType: {"Y": "s", "N": "s"}
                    },
                    callback: {
                        onRightClick: onRightClick

                    }
                }, res.menulists); //初始化树
                zTreeObj.expandAll(true);   //true 节点全部展开、false节点收缩
            }
        });
    });
});
function refresh(){
    $.ajax({
        type: "get",
        url: "/intermediate/gettree",
        async: true,
        success: function (res) {
            a =  res.menulists;
            zTreeObj = $.fn.zTree.init($("#treeDemo"), {
                async: {
                    enable: true
                },
                edit: {
                    // enable: true,
                    drag: {
                        isMove: true, // 打开移动功能，鼠标左键点击后拖拽
                        prev: true,
                        autoOpenTime: 0
                    },
                    editNameSelectAll: true,
                    showRemoveBtn: true,
                    removeTitle: "删除节点",
                    renameTitle: "编辑节点名称",
                    showRenameBtn: true,
                },
                view: {
                    selectedMulti: false,
                    showIcon: true,// 显示图标
                    showLine: true,
                    // addHoverDom: addHoverDom
                    // removeHoverDom : removeHoverDom
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: 'id',
                        pIdKey: 'pid',
                        rootPId: 0
                    },
                    key: {
                        name: "name"
                    }
                },
                check: {
                    enable: true,
                    chkStyle: "checkbox",
                    chkboxType: {"Y": "s", "N": "s"}
                },
                callback: {
                    onRightClick: onRightClick
                }
            }, a); //初始化树
            zTreeObj.expandAll(true);   //true 节点全部展开、false节点收缩
        }
    });
}
function del(){
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    hideRMenu();
    var id1=$("#ak").val();
    console.log(1);
    if (confirm("是否删除？")) {  // 询问是否删除，若删除，则利用Ajax 技术异步删除，同是返回json格式数据，告知前台是否删除成功！
        $.ajax({
            url: "/primary/del",
            dataType: 'json',
            type: 'post',
            data: {"id": id1},
            success: function (response) {
                console.log(treenode11);
                zTree.removeNode(treenode11);     // z-Tree 的api ，从视角上 删除
                alert(response.msg);
            },
            error: function (response) {
                alert(response.msg);
                return false;
            }
        });
    }
    return;
}
function edit(){
    var pid3=$("#fileNode_pid").val();
    var title1=$("#fileNode_pname").val();
    var id1=$("#ak").val();
    layer.open({
        type: 2,
        title: '修改节点',
        content: "treeedit.html",
        area: ['80%', '80%'],
        success:function(layero,index){
            $("#rMenu").css({ "visibility": "hidden"});
            let childFrame=layer.getChildFrame('body',index);
            childFrame.find('input[name="pid"]').val(pid3);
            childFrame.find('input[name="name"]').val(title1);
            childFrame.find('input[name="id"]').val(id1);

        }
    })
}
function addNode(){
    var id2=$("#ak").val();
    layer.open({
        type: 2,
        title: '添加节点',
        content: "treeadd.html",
        area: ['80%', '80%'],
        success:function(layero,index){
            $("#rMenu").css({ "visibility": "hidden"});
            let childFrame=layer.getChildFrame('body',index);
            childFrame.find('input[name="pid"]').val(id2);
        }
    })
}
function onRightClick(event, treeId, treeNode) {
    treenode11=treeNode;
    // console.log(treeNode);
    $("#fileNode_pid").val(treeNode.pid);
    $("#fileNode_pname").val(treeNode.name);
    $("#ak").val(treeNode.id);
    if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
        showRMenu("root", event.clientX, event.clientY);
    } else if (treeNode && !treeNode.noR) {
        showRMenu("node", event.clientX, event.clientY);
    }
};
//显示右键菜单
function showRMenu(type, x, y) {
    $("#rMenu").show();
    $("#rMenu").css({"top": y + "px", "left": x + "px", "visibility": "visible"}); //设置右键菜单的位置、可见
    $("body").bind("mousedown", onBodyMouseDown);
}
//隐藏右键菜单
function hideRMenu() {
    $("#rMenu").css({"visibility": "hidden"}); //设置右键菜单不可见
    $("body").unbind("mousedown", onBodyMouseDown);
}
//鼠标按下事件
function onBodyMouseDown(event) {
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
        // $("#rMenu").hide();
        $("#rMenu").css({ "visibility": "hidden"});
    }
}
$("#selectadd").click(function () {
    layer.open({
        type: 2,
        title: '添加',
        content: "addcourse.html",
        area: ['80%', '80%'],
    })
})
